package logic

import (
	"context"
	"rpc/goods"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddGoodsLogic) AddGoods(req *types.AddGoodsRequest) (resp *types.ResMsgResponse, err error) {
	// todo: 添加商品
	//模拟用户登录
	userId := 1

	add, err := l.svcCtx.GoodsSrv.GoodsAdd(l.ctx, &goods.AddGoodsRequest{
		GoodsTitle:  req.GoodsTitle,
		GoodsPrice:  float32(req.GoodsPrice),
		GoodsStock:  req.GoodsStock,
		GoodsStatus: req.GoodsStatus,
		GoodsAvatar: req.GoodsAvatar,
		UserId:      int64(userId),
	})
	if err != nil {
		return &types.ResMsgResponse{
			Cre:  -1,
			Msg:  "服务器错误",
			Data: nil,
		}, err
	}

	return &types.ResMsgResponse{
		Cre: 0,
		Msg: "商品新增成功",
		Data: map[string]interface{}{
			"goodsId": add.GoodsId,
		},
	}, err
}
